using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using Common;
using Common.Enum;
using SProject.BasicInformation;
using SSettings;

namespace ProjectView.ProjectControls
{
	public partial class UCProjectTarget : UserControl
	{
		public static readonly DependencyProperty TypeProperty = DependencyProperty.Register("Type", typeof(EProjectType), typeof(UCProjectTarget), new UIPropertyMetadata(EProjectType.AQ));

		public static readonly DependencyProperty IsReadOnlyProperty = DependencyProperty.Register("IsReadOnly", typeof(bool), typeof(UCProjectTarget), new PropertyMetadata(false, OnIsReadOnlyChanged));

		public static readonly DependencyProperty TargetProperty = DependencyProperty.Register("Target", typeof(Target), typeof(UCProjectTarget), new PropertyMetadata(null, OnTargetChanged));

		private ObservableCollection<AQTargetDataRow> _ListAQTarget = new ObservableCollection<AQTargetDataRow>();

		private ObservableCollection<GridCheckBoxCell> _CheckCellList = new ObservableCollection<GridCheckBoxCell>();

		private ChannelSetting _ChannelSetting = ConfigSettings.GetInstance().ChannelSetting;

		private string _StringNA = Utility.GetMessage("NA");

		private int[] _Channels = new int[6] { 1, 2, 3, 4, 0, 0 };

		public DataGrid GridControl
		{
			get
			{
				return dataGrid;
			}
		}

		public bool IsReadOnly
		{
			get
			{
				return (bool)GetValue(IsReadOnlyProperty);
			}
			set
			{
				SetValue(IsReadOnlyProperty, value);
			}
		}

		public EProjectType Type
		{
			get
			{
				return (EProjectType)GetValue(TypeProperty);
			}
			set
			{
				SetValue(TypeProperty, value);
			}
		}

		public Target Target
		{
			get
			{
				return (Target)GetValue(TargetProperty);
			}
			set
			{
				SetValue(TargetProperty, value);
			}
		}

		public ObservableCollection<AQTargetDataRow> ListAQTarget
		{
			get
			{
				return _ListAQTarget;
			}
			set
			{
				_ListAQTarget = value;
			}
		}

		public ObservableCollection<GridCheckBoxCell> CheckCellList
		{
			get
			{
				return _CheckCellList;
			}
			set
			{
				_CheckCellList = value;
			}
		}

		public List<List<string>> Dyes { get; set; }

		public List<string> DefaultTargetNames { get; set; }

		private static void OnIsReadOnlyChanged(object sender, DependencyPropertyChangedEventArgs args)
		{
		}

		private static void OnTargetChanged(object sender, DependencyPropertyChangedEventArgs args)
		{
			UCProjectTarget uCProjectTarget = sender as UCProjectTarget;
			uCProjectTarget.SetTargetToWin();
		}

		public UCProjectTarget()
		{
			InitializeComponent();
			string[] array = ConfigReader.GetInstance().GetChannels().Split('&');
			for (int i = 0; i < _Channels.Count(); i++)
			{
				_Channels[i] = Convert.ToInt32(array[i]);
			}
			Dyes = new List<List<string>>
			{
				new List<string>(_ChannelSetting.AllChannelProperty[_Channels[0]].DyeList),
				new List<string>(_ChannelSetting.AllChannelProperty[_Channels[1]].DyeList),
				new List<string>(_ChannelSetting.AllChannelProperty[_Channels[2]].DyeList),
				new List<string>(_ChannelSetting.AllChannelProperty[_Channels[3]].DyeList),
				new List<string>(_ChannelSetting.AllChannelProperty[_Channels[4]].DyeList),
				new List<string>(_ChannelSetting.AllChannelProperty[_Channels[5]].DyeList)
			};
			DefaultTargetNames = new List<string> { _StringNA };
			for (int j = 0; j < 6; j++)
			{
				GridCheckBoxCell gridCheckBoxCell = new GridCheckBoxCell
				{
					Value = false
				};
				gridCheckBoxCell.IsEnable = _ChannelSetting.AllChannelProperty[_Channels[j]].IsEnabled;
				gridCheckBoxCell.Index = j + 1;
				gridCheckBoxCell.PropertyChanged += checkCell_PropertyChanged;
				CheckCellList.Add(gridCheckBoxCell);
			}
			AQTargetDataRow aQTargetDataRow = new AQTargetDataRow
			{
				ColumnItem = Utility.GetMessage("Prj_Excitation-Emission")
			};
			for (int k = 0; k < 6; k++)
			{
				aQTargetDataRow.SetValue(k + 1, _ChannelSetting.AllChannelProperty[_Channels[k]].IsEnabled ? _ChannelSetting.AllChannelProperty[_Channels[k]].WaveLength : _StringNA);
			}
			_ListAQTarget.Add(aQTargetDataRow);
			aQTargetDataRow = new AQTargetDataRow
			{
				ColumnItem = Utility.GetMessage("Prj_Dye")
			};
			for (int l = 0; l < 6; l++)
			{
				aQTargetDataRow.SetValue(l + 1, (Dyes[l].Count == 0) ? string.Empty : Dyes[l][0]);
			}
			_ListAQTarget.Add(aQTargetDataRow);
			_ListAQTarget.Add(new AQTargetDataRow(Utility.GetMessage("Prj_Select"), CheckCellList[0], CheckCellList[1], CheckCellList[2], CheckCellList[3], CheckCellList[4], CheckCellList[5]));
			_ListAQTarget.Add(new AQTargetDataRow(Utility.GetMessage("Prj_TubeDispaly") + 1, "", "", "", "", "", "")
			{
				TubeNo = 1
			});
			for (int m = 1; m <= 6; m++)
			{
				DataGridTemplateColumn item = new DataGridTemplateColumn
				{
					Header = m,
					CellTemplateSelector = new DataGridCellSelector(),
					CellEditingTemplateSelector = new DataGridCellEditSelector()
				};
				dataGrid.Columns.Add(item);
			}
			dataGrid.ItemsSource = _ListAQTarget;
		}

		private void checkCell_PropertyChanged(object sender, PropertyChangedEventArgs e)
		{
			GridCheckBoxCell gridCheckBoxCell = sender as GridCheckBoxCell;
			if (gridCheckBoxCell == null || !e.PropertyName.Equals("Value"))
			{
				return;
			}
			if (!IsReadOnly && !gridCheckBoxCell.Value && MessageBox.Show(Utility.GetMessage("Msg_Modify_Delete_Info_Confirm"), Utility.GetMessage("Warning"), MessageBoxButton.YesNo, MessageBoxImage.Exclamation) == MessageBoxResult.No)
			{
				gridCheckBoxCell.Value = true;
				return;
			}
			List<GridCheckBoxCell> list = null;
			switch (Type)
			{
			case EProjectType.HRM:
				list = CheckCellList.Where((GridCheckBoxCell s) => s.Value).ToList();
				if (list.Count > 1)
				{
					foreach (GridCheckBoxCell item in list)
					{
						if (gridCheckBoxCell != item)
						{
							item.PropertyChanged -= checkCell_PropertyChanged;
							item.Value = false;
							item.PropertyChanged += checkCell_PropertyChanged;
							_ListAQTarget[3].SetValue(item.Index, "");
							_ListAQTarget[3].SetColor(item.Index, Utility.GetTargetColor(1, item.Index));
						}
					}
				}
				gridCheckBoxCell.PropertyChanged -= checkCell_PropertyChanged;
				gridCheckBoxCell.Value = true;
				gridCheckBoxCell.PropertyChanged += checkCell_PropertyChanged;
				break;
			case EProjectType.AD:
				list = CheckCellList.Where((GridCheckBoxCell s) => s.Value).ToList();
				if (list.Count > 2)
				{
					gridCheckBoxCell.PropertyChanged -= checkCell_PropertyChanged;
					gridCheckBoxCell.Value = false;
					gridCheckBoxCell.PropertyChanged += checkCell_PropertyChanged;
				}
				break;
			}
			if (gridCheckBoxCell.Value)
			{
				dataGrid.CurrentColumn = dataGrid.Columns[gridCheckBoxCell.Index];
				dataGrid.CurrentItem = _ListAQTarget[3];
				dataGrid.BeginEdit();
			}
			else
			{
				ClearData(gridCheckBoxCell.Index);
			}
		}

		private void ClearData(int idx)
		{
			for (int i = 3; i < _ListAQTarget.Count; i++)
			{
				_ListAQTarget[i].SetValue(idx, "");
				_ListAQTarget[i].SetColor(idx, Utility.GetTargetColor(i - 2, idx));
			}
		}

		private void SetTargetToWin()
		{
			dataGrid.CancelEdit();
			if (Target != null)
			{
				bool flag = true;
				if (!string.IsNullOrEmpty(Target.Channels))
				{
					for (int i = 0; i < _Channels.Count(); i++)
					{
						string[] array = Target.Channels.Split('&');
						int result = -1;
						if (int.TryParse(array[i], out result) && result != _Channels[i])
						{
							_Channels[i] = result;
							flag = false;
						}
					}
				}
				if (!flag)
				{
					for (int j = 0; j < Dyes.Count; j++)
					{
						if (_Channels[j] < _ChannelSetting.AllChannelProperty.Count)
						{
							Dyes[j] = _ChannelSetting.AllChannelProperty[_Channels[j]].DyeList;
							CheckCellList[j].IsEnable = _ChannelSetting.AllChannelProperty[_Channels[j]].IsEnabled;
							_ListAQTarget[0].SetValue(j + 1, _ChannelSetting.AllChannelProperty[_Channels[j]].IsEnabled ? _ChannelSetting.AllChannelProperty[_Channels[j]].WaveLength : _StringNA);
							_ListAQTarget[1].SetValue(j + 1, (Dyes[j].Count == 0) ? string.Empty : Dyes[j][0]);
						}
					}
				}
				int tubeCount = Target.GetTubeCount();
				if (tubeCount > 0)
				{
					SetTubeCount(tubeCount);
					int k;
					for (k = 1; k <= 6; k++)
					{
						GridCheckBoxCell gridCheckBoxCell = _ListAQTarget[2].GetObject(k) as GridCheckBoxCell;
						IEnumerable<TargetItem> source = Target.Items.Where((TargetItem item) => item.ChannelNo.ToString().Equals(k.ToString()));
						gridCheckBoxCell.Value = source.Count() > 0;
						if (gridCheckBoxCell.Value)
						{
							for (int l = 3; l < _ListAQTarget.Count; l++)
							{
								_ListAQTarget[l].SetValue(k, _StringNA);
								_ListAQTarget[l].SetColor(k, Utility.GetTargetColor(l - 2, k));
							}
						}
					}
					for (int m = 0; m < Target.Items.Count; m++)
					{
						TargetItem targetItem = Target.Items[m];
						_ListAQTarget[1].SetValue(targetItem.ChannelNo, targetItem.Dye);
						_ListAQTarget[2 + targetItem.TubeNo].ColumnItem = targetItem.TubeName;
						_ListAQTarget[2 + targetItem.TubeNo].SetValue(targetItem.ChannelNo, targetItem.Name);
						_ListAQTarget[2 + targetItem.TubeNo].SetColor(targetItem.ChannelNo, targetItem.Color);
					}
					return;
				}
			}
			SetTubeCount(1);
			for (int n = 1; n <= 6; n++)
			{
				GridCheckBoxCell gridCheckBoxCell2 = _ListAQTarget[2].GetObject(n) as GridCheckBoxCell;
				if (gridCheckBoxCell2 != null)
				{
					gridCheckBoxCell2.PropertyChanged -= checkCell_PropertyChanged;
					gridCheckBoxCell2.Value = false;
					gridCheckBoxCell2.PropertyChanged += checkCell_PropertyChanged;
					for (int num = 3; num < _ListAQTarget.Count; num++)
					{
						_ListAQTarget[num].SetValue(gridCheckBoxCell2.Index, "");
						_ListAQTarget[num].SetColor(gridCheckBoxCell2.Index, Utility.GetTargetColor(num - 2, gridCheckBoxCell2.Index));
					}
				}
			}
		}

		public bool SaveTarget(bool showMsg = true)
		{
			new StringBuilder();
			if (showMsg)
			{
				dataGrid.CommitEdit();
			}
			for (int num = Target.Items.Count - 1; num >= 0; num--)
			{
				TargetItem targetItem = Target.Items[num];
				if (!CheckCellList[targetItem.ChannelNo - 1].Value)
				{
					Target.Items.Remove(targetItem);
				}
				else if (targetItem.TubeNo > _ListAQTarget.Count - 3)
				{
					Target.Items.Remove(targetItem);
				}
			}
			for (int i = 0; i < CheckCellList.Count; i++)
			{
				int channelNo = i + 1;
				GridCheckBoxCell gridCheckBoxCell = CheckCellList[i];
				if (!gridCheckBoxCell.Value)
				{
					continue;
				}
				bool flag = true;
				int j;
				for (j = 3; j < _ListAQTarget.Count; j++)
				{
					string text = _ListAQTarget[j].GetObject(channelNo).ToString();
					if (text.Length == 0)
					{
						if (showMsg)
						{
							dataGrid.UnselectAllCells();
							MessageBox.Show(Utility.GetMessage("msg_not_input_targetname"));
							dataGrid.CurrentColumn = dataGrid.Columns[i + 1];
							dataGrid.CurrentItem = _ListAQTarget[j];
							dataGrid.BeginEdit();
						}
						return false;
					}
					if (text == _StringNA)
					{
						TargetItem targetItem2 = Target.Items.Where((TargetItem s) => s.TubeNo == j - 2 && s.ChannelNo == channelNo).FirstOrDefault();
						if (targetItem2 != null)
						{
							Target.Items.Remove(targetItem2);
						}
						continue;
					}
					for (int k = j; k < _ListAQTarget.Count; k++)
					{
						for (int l = 0; l < CheckCellList.Count; l++)
						{
							if ((k != j || (k == j && l != i)) && CheckCellList[l].Value && text == _ListAQTarget[k].GetObject(l + 1).ToString())
							{
								if (showMsg)
								{
									dataGrid.UnselectAllCells();
									MessageBox.Show(Utility.GetMessage("msg_same_targetname"));
									dataGrid.CurrentColumn = dataGrid.Columns[i + 1];
									dataGrid.CurrentItem = _ListAQTarget[j];
									dataGrid.BeginEdit();
								}
								return false;
							}
						}
					}
					TargetItem targetItem3 = Target.Items.Where((TargetItem s) => s.TubeNo == j - 2 && s.ChannelNo == channelNo).FirstOrDefault();
					if (targetItem3 == null)
					{
						Target.Items.Add(new TargetItem
						{
							ChannelNo = channelNo,
							TubeNo = j - 2,
							Name = text,
							TubeName = _ListAQTarget[j].ColumnItem.ToString(),
							Dye = _ListAQTarget[1].GetObject(channelNo).ToString(),
							Color = _ListAQTarget[j].GetColor(channelNo)
						});
					}
					else
					{
						targetItem3.Name = text;
						targetItem3.TubeName = _ListAQTarget[j].ColumnItem.ToString();
						targetItem3.Dye = _ListAQTarget[1].GetObject(channelNo).ToString();
						targetItem3.Color = _ListAQTarget[j].GetColor(channelNo);
					}
					flag = false;
				}
				if (flag)
				{
					if (showMsg)
					{
						dataGrid.UnselectAllCells();
						MessageBox.Show(Utility.GetMessage("msg_not_input_targetname"));
						dataGrid.CurrentColumn = dataGrid.Columns[i + 1];
						dataGrid.CurrentItem = _ListAQTarget[3];
						dataGrid.BeginEdit();
					}
					return false;
				}
			}
			if (Target.Items.Count == 0)
			{
				if (showMsg)
				{
					dataGrid.UnselectAllCells();
					MessageBox.Show(Utility.GetMessage("msg_not_input_target"));
					dataGrid.CurrentColumn = dataGrid.Columns[1];
					dataGrid.CurrentItem = _ListAQTarget[2];
					dataGrid.SelectCell(2, 1);
				}
				return false;
			}
			if (Type == EProjectType.AD && Target.Items.Count != 2)
			{
				if (showMsg)
				{
					MessageBox.Show(Utility.GetMessage("msg_ad_not_engouh_target"));
				}
				return false;
			}
			return true;
		}

		public void SetTubeCount(int cnt)
		{
			if (cnt <= 0)
			{
				return;
			}
			dataGrid.CommitEdit();
			int num = _ListAQTarget.Count - 3;
			string message = Utility.GetMessage("Prj_TubeDispaly");
			if (cnt > num)
			{
				for (int i = num; i < cnt; i++)
				{
					_ListAQTarget.Add(new AQTargetDataRow(message + (i + 1))
					{
						TubeNo = i + 1
					});
				}
			}
			else if (num > cnt)
			{
				for (int num2 = num; num2 > cnt; num2--)
				{
					_ListAQTarget.RemoveAt(num2 + 3 - 1);
				}
			}
		}

		public void UnSelectAll()
		{
			dataGrid.UnselectAllCells();
		}

		private void DataGrid_BeginningEdit(object sender, DataGridBeginningEditEventArgs e)
		{
			if (IsReadOnly)
			{
				e.Cancel = true;
				return;
			}
			if (e.Column.DisplayIndex == 0)
			{
				if (e.Row.GetIndex() < 3)
				{
					e.Cancel = true;
				}
				return;
			}
			if (!_ChannelSetting.AllChannelProperty[_Channels[e.Column.DisplayIndex - 1]].IsEnabled)
			{
				e.Cancel = true;
				return;
			}
			switch (e.Row.GetIndex())
			{
			case 0:
			case 2:
				e.Cancel = true;
				return;
			case 1:
				return;
			}
			if (!CheckCellList[e.Column.DisplayIndex - 1].Value)
			{
				e.Cancel = true;
			}
		}
	}
}
